<?php
	require('../_db.php');
	
	define('DEBUG',false);
	
	include('facebook_categories.php');
	
	/*if (!isset($_POST['id_rec']) ||
	!is_numeric($_POST['id_rec'])) exit(-1);*/
	
	if (!isset($_GET['id_rec']) ||
	!is_numeric($_GET['id_rec'])) exit(-1);
	
	//$_POST['id_excl'] = mysql_real_escape_string($_POST['id_excl']);
	
	// enter database info
	/*$q = " SELECT `nume`, `poza`, `descriere`, `link` FROM cadouri
	WHERE 1 ORDER BY rand() LIMIT 1 ";
	if ($ok = mysql_query($q))
	{
		if ($row = mysql_fetch_array($ok))
		{
			echo json_encode($row);
		}
	}		
	else echo -1;*/
	
	// calcul prioritati categorii
	
	$myCats = array();
	$myScores = array();
	$fb_gender = '';
	$q = " SELECT l.facebook_page_id as fb_id, l.facebook_category as fb_cat, r.fb_gender FROM user_likes l, recomandari r WHERE r.fb_friend_id = l.facebook_user_id AND r.id = '".$_GET['id_rec']."'";
	if ($ok = mysql_query($q))
	{
		while ($row = mysql_fetch_array($ok))
		{
			array_push($myCats, $row);
			$myScores[$fb_cats[strtolower($row['fb_cat'])]] = 0;
			if (!$fb_gender) $fb_gender = $row['fb_gender'];
		}
	}
	else die(mysql_error());
	//var_dump($myCats);
	if ($fb_gender == 'male') $fb_gender = 'M';
	else $fb_gender = 'F';
	
	foreach($myCats as $cat)
	{
		$myScores[$fb_cats[strtolower($cat['fb_cat'])]]++;
	}
	arsort($myScores);
	
	if (DEBUG == true)
	var_dump($myScores);
	
	$ourCats = array();
	$q = " SELECT * FROM categorii";
	if ($ok = mysql_query($q))
	{
		while ($row = mysql_fetch_array($ok))
		{
			array_push($ourCats, $row);
		}
	}
	else die(mysql_error());
	//var_dump($ourCats);
	
	
	// extragere scoruri
	$scoruri = array();
	$q = " SELECT scor FROM atribute_recomandari WHERE id_recomandare = '".$_GET['id_rec']."'";
	if ($ok = mysql_query($q))
	{
		while ($row = mysql_fetch_array($ok))
		{
			array_push($scoruri, $row);
		}
	}
	else die(mysql_error());
	
	if (DEBUG == true)
	var_dump($scoruri);
	
	
	// SOCIAL
	$matScor = array();
	foreach($scoruri as $scor)
	{
		$arScor = explode(',',$scor[0]);
		array_push($matScor, $arScor);
	}
	if (DEBUG == true)
	var_dump($matScor);
	
	$sumScor = array();
	$numeCategorii = array();
	$q = " SELECT id, nume FROM categorii ";
	if ($ok = mysql_query($q))
	{
		while ($row = mysql_fetch_array($ok))
		{
			array_push($myCats, $row);
			$numeCategorii[$row['id']-1] = $row['nume'];
			$sumScor[$row['id']-1] = 1;
		}
	}
	else die(mysql_error());
		
	foreach($matScor as $i => $oneScor)
	{
		//echo 'linia ';
		//var_dump($oneScor);
		foreach($oneScor as $j => $oneElem)
		{
			global $sumScor;
			$sumScor[$j] += $oneElem-1;
		}
	}
	
	if (DEBUG == true)
	var_dump($sumScor);
	
	if (DEBUG == true)
	var_dump($fb_cats);
	
	if (DEBUG == true)
	var_dump($sumScor);
	
	for ($i = 0; $i < 7; $i++)
	{
		if (DEBUG == true)
		echo 'Inmultesc '.$sumScor[$i].' cu '.$myScores[$numeCategorii[$i]]."(categoria ".$numeCategorii[$i].") \n\r";
		$sumScor[$i] = floatVal($sumScor[$i] * $myScores[$numeCategorii[$i]]);
	}
	
	if (DEBUG == true)
	var_dump($sumScor);
	
	if (DEBUG == true)
	var_dump($numeCategorii);
	
	arsort($sumScor);
	
	if (DEBUG == true)
	var_dump($sumScor);
	
	$cadouri = array();
	$k = 0;
	foreach ($sumScor as $id_cat => $scor)
	{
		if ($k == 3) break;
		$k++;
		$q = " SELECT c.`id`, c.`nume`, c.`poza`, c.`descriere`, c.`link` FROM cadouri c, atribute_cadouri a
			WHERE c.id_cat = ".($id_cat+1)." AND a.id_atribut = 4 AND a.valoare LIKE '%".$fb_gender."%' AND a.id_cadou = c.id ORDER BY rand() LIMIT 1 ";
		if (DEBUG == true)
		echo $q."\n\r";
		if ($ok = mysql_query($q))
		{
			if ($row = mysql_fetch_array($ok))
			{
				array_push($cadouri, $row);
			}
		}		
	}
	
	echo json_encode($cadouri);
	
	if (DEBUG == true)
	var_dump($cadouri);
	
	